/**  File: fsm.cpp
 *   Author: Jaideep Balekar
 *           Department of Computer Science
 *           Texas A&M University
 *           315 - Programming Studio
 *   Due Date: December 6th, 2009 @ 11:59
 *   Contents: This class emulates a finite state machine that's being used to store game states and the relationship of states with each other.
 */
#include "fsm.h"
#include <QDebug>

FSM::FSM()
{
}

FSM::~FSM()
{
}

void FSM::update()
{
    if(stateStatus == ENTRY)
        stateStatus = IN;
    else if(stateStatus == EXIT)
    {
        prevState = currState;
        currState = newState;
        stateStatus = ENTRY;
    }
}

void FSM::addState(State &stateObject)
{
    QString stateName = stateObject.getName();
    statemap.insert(stateName,stateObject);
}

QString FSM::getState()
{
    State s = statemap.value(currState);
    return s.getName();
}

int FSM::getStateIndex()
{
    State s = statemap.value(currState);
    return s.getIndex();
}

bool FSM::hasProperty(QString propertyName)
{
    State s = statemap.value(currState);
    return s.hasProperty(propertyName);
}

void FSM::handleEvent(QString eventName)
{
    State s = statemap.value(currState);
    newState = s.getNextStateForEvent(eventName);
    stateStatus = EXIT;
}

void FSM::setInitialState(QString name)
{
    currState = name;
    stateStatus = ENTRY;
}

bool FSM::inENTRY()
{
    if(stateStatus == ENTRY)
        return true;
    else
        return false;
}

bool FSM::inIN()
{
    if(stateStatus == IN)
        return true;
    else
        return false;
}

bool FSM::inEXIT()
{
    if(stateStatus == EXIT)
        return true;
    else
        return false;
}



